﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MaterialDesignThemes.Wpf;
using Notification.Wpf;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace GameView.Comment
{
    public sealed partial class GlobalMessage : ObservableRecipient
    {
        public static GlobalMessage Instance { get; } = new();

        private GlobalMessage()
        {
            // 注册消息接收（如果需要）
            Messenger.Register<NotificationRequestMessage>(this, (r, m) =>
            {
                Show(m.Message, m.Type, m.DurationSeconds);
            });
        }

        [ObservableProperty]
        private int _defaultDurationSeconds = 8;

        [ObservableProperty]
        private int _unreadCount = 0;

        [ObservableProperty]
        private ObservableCollection<NotificationContent> _history = new();

        private readonly NotificationManager _notificationManager = new();

        private static readonly IReadOnlyDictionary<NotificationType, string> TitleMapping = new Dictionary<NotificationType, string>
        {
            [NotificationType.None] = "通知",
            [NotificationType.Information] = "信息",
            [NotificationType.Success] = "操作成功",
            [NotificationType.Warning] = "警告",
            [NotificationType.Error] = "错误",
            [NotificationType.Notification] = "系统消息"
        };

        private static readonly IReadOnlyDictionary<NotificationType, PackIconKind> IconMapping = new Dictionary<NotificationType, PackIconKind>
        {
            [NotificationType.Success] = PackIconKind.CheckCircle,
            [NotificationType.Error] = PackIconKind.CloseCircle,
            [NotificationType.Warning] = PackIconKind.AlertCircle,
            [NotificationType.Information] = PackIconKind.Information,
            [NotificationType.Notification] = PackIconKind.Bell,
        };

        public void ShowWarning(string message) => Show(message, NotificationType.Warning);

        public void ShowError(string message) => Show(message, NotificationType.Error);

        public void ShowInformation(string message) => Show(message, NotificationType.Information);

        public void ShowSuccess(string message) => Show(message, NotificationType.Success);

        public void Show(string messageX, NotificationType type, int? durationSeconds = null)
        {
            string message = string.Empty;
            try
            {
                var str = Regex.Unescape(messageX);
                message = str;
            }
            catch
            {
                message = messageX;
            }
            var content = new NotificationContent
            {
                Title = TitleMapping[type],
                Message = $"{message}\n【{DateTime.Now:yyyy-MM-dd HH:mm:ss}】",
                Type = type,
                Foreground = Brushes.White,
                Icon = new PackIcon { Kind = IconMapping[type] },
            };
            _notificationManager.Show(content, expirationTime: TimeSpan.FromSeconds(durationSeconds ?? DefaultDurationSeconds));
            if (History.Count >= 1000)
            {
                History.RemoveAt(History.Count - 1);
            }
            History.Insert(0, content);
            UnreadCount++;
        }

        [RelayCommand]
        private void ClearHistory()
        {
            History.Clear();
            UnreadCount = 0;
        }
    }

    /// <summary>
    /// 用于通过消息系统请求通知
    /// </summary>
    public sealed record NotificationRequestMessage(
        string Message,
        NotificationType Type = NotificationType.Information,
        int DurationSeconds = 5);
}